I started (trying) to learn OOP. Failed. Now I need help! D
Hello, I'm trying to learn OOP trough pygame. Im programing (trying:D) a snake game and I'm stuck at the very beginning :( If you could look over my code real quick and tell me why it's not drawing the rectangle I would be very glad.
<pre class='prettyprint lang-py'> import pygame WHITE = (255,255,255)
import pygame WHITE = (255,255,255)
class init_window(): FPS = 60 clock = pygame.time.Clock() pygame.init()
if __name__ == '__main__': init_window() init_window.draw_snake(snake)
I have no problem doing that without OOP concepts thanks to your pygame series but I'm totally stuck here.
Thanks in advance!
You must be logged in to post. Please login or register an account.
At least with a brief look, we know right out of the gate the following runs:
snake = init_snake(WHITE, 50, 50)
if __name__ == '__main__': init_window() init_window.draw_snake(snake)
So we are running the init_snake first, which simply makes some definitions, but doesn't do anything.
Next, your if statement rings true, you run the init_window(), which also makes some definitions, but doesn't do anything.
Next, you do init_window.draw_snake, now you have some things happening, and you even drew some stuff!
Where you've gone wrong thus far fundamentally is not necessarily OOP. You actually drew something, but you didn't update the screen. After doing all changes in PyGame, you need to run an update with pygame.display.update()
-Harrison 9 years ago
You must be logged in to post. Please login or register an account.
Thanks for your quick reply but I forgot to mention that I'm also getting an error when running the programm:
pygame.draw.rect(self.gameDisplay, snake) AttributeError: 'init_snake' object has no attribute 'gameDisplay'
-Bobby264 9 years ago
Last edited 9 years ago
You must be logged in to post. Please login or register an account.
Oh and nice update on the website btw :)
-Bobby264 9 years ago
You must be logged in to post. Please login or register an account.
Thanks! Been working on the updates for some time, nice to finally push them out!
-Harrison 9 years ago
You must be logged in to post. Please login or register an account.
OOP is fundamentally different than something like functional programming. You're kind of trying to treat OOP as if it were functional, and that's just not the paradigm.
For example, you're trying to pass a snake object through, as if it was a parameter, or so it appears to me, and that's just not how it works.
Also, you never actually assign an object. OOP is meant for you to create objects, rather than use functions to do things. So, when you create a class, the "return" of a class is an object.
Thus, when you call upon a class, you almost always are going to assign the return to a variable, and this is your object. You never did this either, and that is causing trouble. Those are really your two major flaws.
I have cleaned your code a bit:
import pygame WHITE = (255,255,255)
class Init_Window: FPS = 60 clock = pygame.time.Clock() pygame.init()
The main note here is to even begin, you must create an object. You do this with the window = part. Then, you can begin to use/modify that object's attributes with "window.ATTRIBUTE." You were attempting to just straight call upon the attributes directly from the class, which doesn't work.
That should mostly be it.
One final note is I combined this to be one class. If you wanted to mesh two classes, then you are almost always going to be using inheritance. I would suggest you try to work with just one class until you are comfortable, then dive into the idea of inheritance, building classes on top of other classes.
At some point this year, I will be doing an in-depth series on OOP. Til then, hope this helps!
-Harrison 9 years ago
You must be logged in to post. Please login or register an account.
Of course it helps! I'm looking forward to see this series :) Thanks for all your efforts and for providing such great learning material.
-Bobby264 9 years ago
You must be logged in to post. Please login or register an account.